//
//  HZMOthersTVCell.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/30.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMOthersTVCell.h"
#import <UIImageView+WebCache.h>
#define HWIDTH self.headImageView.frame.size.width
#define HHEIGHT self.headImageView.frame.size.height
#define FWIDTH self.footView.frame.size.width
#define FHEIGHT self.footView.frame.size.height
#import "SaveTool.h"
@implementation HZMOthersTVCell


- (void)dealloc
{
    [_headImageView release];
    [_userImageView release];
    [_userLabel release];
    [_timeLabel release];
    [_picImageView release];
    [_contentLabel release];
    [_shareImageView release];
    [_commentImageView release];
    [_likesButton release];
    [_footView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    self.headImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headImageView];
    [_headImageView release];
    
    self.userImageView = [[UIImageView alloc] init];
    [self.headImageView addSubview:self.userImageView];
    [_userImageView release];
    self.userImageView.layer.cornerRadius = 20;
    self.userImageView.layer.masksToBounds = YES;
    
    self.userLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.userLabel];
    [_userLabel release];
    self.userLabel.font = [UIFont systemFontOfSize:13.0];
    
    self.timeLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.timeLabel];
    [_timeLabel release];
    self.timeLabel.font = [UIFont systemFontOfSize:11.0];
    self.timeLabel.textColor = [UIColor grayColor];
    
    self.picImageView = [[UIImageView alloc] init];
    [self.headImageView addSubview:self.picImageView];
    [_picImageView release];
    
    self.contentLabel = [[UILabel alloc] init];
    [self.headImageView addSubview:self.contentLabel];
    [_contentLabel release];
    self.contentLabel.textColor = [UIColor grayColor];
    self.contentLabel.font = [UIFont systemFontOfSize:15.0];
    self.contentLabel.numberOfLines = 0;
    [self.contentLabel sizeToFit];
    
    
    self.footView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.footView];
    [_footView release];
    self.footView.userInteractionEnabled = YES;
    
    self.shareImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"share.png"]];
    [self.footView addSubview:self.shareImageView];
    [_shareImageView release];
    
    self.commentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Comment.png"]];
    [self.footView addSubview:self.commentImageView];
    [_commentImageView release];
    
    
    [self.footView addSubview:self.likesButton];
    
    
    
}


- (UIButton *)likesButton
{
    if (!_likesButton) {
        self.likesButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        [_likesButton addTarget:self action:@selector(cellBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likesButton;
}


- (void)cellBtnClicked:(UIButton *)likeButton
{
    
//    NSLog(@"%@", S)
//    _liked.liked = !_liked.liked;
    _cellModel.liked = !_cellModel.liked;
    if (_cellModel.liked) {
        [_likesButton setImage:[UIImage imageNamed:@"Pay_Attention"] forState:UIControlStateNormal];
        [SaveTool saveListToPlist:self.cellModel];
        
    } else {
        [_likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
        [SaveTool cancelListInPlist:self.cellModel];
    }
    
    
}



#pragma mark lazy load
- (void)setCellArr:(HZMOtherView *)cellModel
{
    if (_cellModel != cellModel) {
        [_cellModel release];
        _cellModel = [cellModel retain];
        [self reload];
    }
}

- (void)setPicModel:(HZMPics *)picModel
{
    if (_picModel != picModel) {
        [_picModel release];
        _picModel = [picModel retain];
        [self reload];
    }
}

- (void)setTagsModel:(HZMTags *)tagsModel
{
    if (_tagsModel != tagsModel) {
        [_tagsModel release];
        _tagsModel = [tagsModel retain];
        [self reload];
    }
    
}

- (void)setAuthorModel:(HZMAuthor *)authorModel
{
    if (_authorModel != authorModel) {
        [_authorModel release];
        _authorModel = [authorModel retain];
        [self reload];
    }
}

- (void)setDynamicModel:(HZMDynamic *)dynamicModel
{
    if (_dynamicModel != dynamicModel) {
        [_dynamicModel release];
        _dynamicModel = [dynamicModel retain];
        [self reload];
    }
}

- (void)reload
{
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:_authorModel.avatar]];
    self.userLabel.text = _authorModel.username;
    self.timeLabel.text = _cellModel.datestr;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:_picModel.picsUrl]];
    self.contentLabel.text = _cellModel.content;
    
    
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    self.headImageView.frame = CGRectMake(0, 5, self.contentView.frame.size.width, 11 * (self.contentView.frame.size.height - 10) / 12);
    
    self.userImageView.frame = CGRectMake(HWIDTH / 140, HWIDTH / 40, 40, 40);
    
    self.userLabel.frame = CGRectMake(5 * HWIDTH / 36, HHEIGHT / 140, 8 * HWIDTH / 9, HHEIGHT / 20);
    
    self.timeLabel.frame = CGRectMake(5 * HWIDTH / 36, HHEIGHT / 32, 8 * HWIDTH / 9, HHEIGHT / 20);
    
    self.picImageView.frame = CGRectMake(0, HHEIGHT / 10, HWIDTH, 7 * HHEIGHT / 10);
    
    self.contentLabel.frame = CGRectMake(HWIDTH / 140, 16 * HHEIGHT / 20, HWIDTH - 20, 2 * HHEIGHT / 10);
    
    
    
    self.footView.frame = CGRectMake(0, (11 * self.contentView.frame.size.height / 12 - 10), self.contentView.frame.size.width, (self.contentView.frame.size.height - 10) / 12);
    
    self.shareImageView.frame = CGRectMake(0, 0, FWIDTH / 3, FHEIGHT);
    
    self.commentImageView.frame = CGRectMake(FWIDTH / 3, 0, FWIDTH / 3, FHEIGHT);
    
    self.likesButton.frame = CGRectMake(2 * self.footView.frame.size.width / 3, 0, self.footView.frame.size.width / 3, self.footView.frame.size.height);

//    if (_cellModel.liked) {
//        [self.likesButton setImage:[UIImage imageNamed:@"Pay_Attention" ] forState:UIControlStateNormal];
//    } else {
//        [self.likesButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
//    }
}












//- (void)cellBtnClicked:(UIButton *)likeButton event:(id)event
//{
//    NSSet *touches = [event allTouches];
//    UITouch *touch = [touches anyObject];
//    CGPoint currentTouchPosition = [touch locationInView:_tableView];
//    self.indexPath = [_tableView indexPathForRowAtPoint:currentTouchPosition];
//    NSLog(@"%ld", self.indexPath.row);
//    if (_indexPath != nil)
//    {
//        // do something
//            if ([likeButton.imageView.image isEqual:[UIImage imageNamed:@"No_Attention"]] /* 这里用db判断 */) {
//                [likeButton setImage:[UIImage imageNamed:@"Pay_Attention"] forState:UIControlStateNormal];
//        
//            } else {
//                [likeButton setImage:[UIImage imageNamed:@"No_Attention"] forState:UIControlStateNormal];
//            }
//  }
//}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
